﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MakeIT_Bill_Maker
{
    public partial class EditReportDetails : Form
    {
        // Data Connection
        private Database.DatabaseConnection db = new Database.DatabaseConnection();
        private DataSet ds;
        private string sql;
        private DataTable dt;

        public EditReportDetails()
        {
            InitializeComponent();
        }

        // Form Load
        private void EditReportDetails_Load(object sender, EventArgs e)
        {
            sql = "SELECT TAddress,TTel,TimeFrom,TimeTo,Days FROM OtherFixed WHERE Index=1";
            ds = db.Show(sql);
            dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                txtTAddress.Text = row[0].ToString();
                txtTTel.Text = row[1].ToString();
                txtTimeFrom.Text = row[2].ToString();
                txtTimeTo.Text = row[3].ToString();
                txtDays.Text = row[4].ToString();
            }
        }

        // 'Edit' button
        private void btnEdit_Click(object sender, EventArgs e)
        {
            string tAddress = txtTAddress.Text;
            string tTel = txtTTel.Text;
            string timeFrom = txtTimeFrom.Text;
            string timeTo = txtTimeTo.Text;
            string days = txtDays.Text;
            if (tAddress == "" || tTel == "" || timeFrom == "" || timeTo == "" || days == "")
            {
                MessageBox.Show("All the fields must be filled.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                sql = "UPDATE OtherFixed SET TAddress = '" + tAddress + "',TTel = '" + tTel + "',TimeFrom = '" + timeFrom + "',TimeTo = '" + timeTo + "',Days = '" + days + "' WHERE Index=1";
                bool b = db.edit(sql);
                if (b == true)
                {
                    MessageBox.Show("Value(s) updated succesfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Failed to update the value(s).", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        // 'Edit' button
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtTAddress.Text = "";
            txtTTel.Text = "";
            txtTimeFrom.Text = "";
            txtTimeTo.Text = "";
            txtDays.Text = "";
        }
    }
}
